package bf.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FactoryService {
	private FactoryDao factoryDao;
	public FactoryService(FactoryDao factoryDao){
		this.factoryDao = factoryDao;
	}
	//랏 구동 처리 로직 시작
	int lotOP1Hp;
	int lotOP2Hp;
	int lotOP3Hp;
	public void setLotInput(int lotInput) throws SQLException {
		lotInput+=factoryDao.getBeforeLotFailCount()*2;
		List<Integer> lotOPHp=factoryDao.getlotOPHp();
		lotOP1Hp=lotOPHp.get(0);lotOP2Hp=lotOPHp.get(1);lotOP3Hp=lotOPHp.get(2);
		int lotListId=factoryDao.getLotListIdByToday();
		if(lotListId==0){
			factoryDao.setLotList();
			lotListId=factoryDao.getLotListIdByToday();
		}
		LotVO lvo=new LotVO(lotInput,lotListId);
		int lotId=factoryDao.setLotNgetLotId(lvo);
		OperationVO ovo=new OperationVO(lotId);
		int lotMount=lotInput/2;
		for(int i=0;i<lotMount;i++){
			setOp1(ovo);
			setOp2(ovo);
			setOp3(ovo);
		}
		double lotOP1Fail=(double)factoryDao.op1FailCount(lotId)/(double)lotMount;
		double lotOP2Fail=(double)factoryDao.op2FailCount(lotId)/(double)lotMount;
		double lotOP3Fail=(double)factoryDao.op3FailCount(lotId)/(double)lotMount;
		double lotFail=(lotOP1Fail+lotOP2Fail+lotOP3Fail)/3;
		int lotOk=factoryDao.getLotOk(lotId);
		lvo.setLot_op1_fail(lotOP1Fail);
		lvo.setLot_op2_fail(lotOP2Fail);
		lvo.setLot_op3_fail(lotOP3Fail);
		lvo.setLot_fail(lotFail);
		lvo.setLot_ok(lotOk);
		lvo.setLot_output(lotMount);
		lvo.setLot_op1_hp(lotOPHp.get(0)-lotOP1Hp);
		lvo.setLot_op2_hp(lotOPHp.get(1)-lotOP2Hp);
		lvo.setLot_op3_hp(lotOPHp.get(2)-lotOP3Hp);
		lvo.setLot_id(lotId);
		factoryDao.setLot(lvo);
		factoryDao.machineAfterLot(lotOP1Hp,lotOP2Hp,lotOP3Hp);
	}
	public void setOp1(OperationVO ovo) throws SQLException{
		ovo.setOp_flag(getFail());
		int opId=factoryDao.setOp1(ovo);
		try {
			Thread.sleep(500);
			lotOP1Hp-=1;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		factoryDao.setOp1End(opId);
	}
	public void setOp2(OperationVO ovo) throws SQLException{
		ovo.setOp_flag(getFail());
		int opId=factoryDao.setOp2(ovo);
		try {
			Thread.sleep(500);
			lotOP2Hp-=1;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		factoryDao.setOp2End(opId);
	}
	public void setOp3(OperationVO ovo) throws SQLException{
		ovo.setOp_flag(getFail());
		int opId=factoryDao.setOp3(ovo);
		try {
			Thread.sleep(500);
			lotOP3Hp-=1;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		factoryDao.setOp3End(opId);
	}
	public boolean getFail(){
		boolean flag=false;
		if(Math.random()<0.9){
			flag=true;
		}
		return flag;
	}
	//랏 구동 처리 로직 끝
	
	// login
	public MemberVO login(MemberVO mvo) throws SQLException{
		return factoryDao.login(mvo);
	}
	// machine정보 모두 가져오는 서비스
	public List getMachineInfo() throws SQLException{
		return (List) factoryDao.getMachineInfo();
	}
	// machine 추가하는 서비스
	public void updateMachine(String mc_id) throws SQLException{
		factoryDao.updateMachine(mc_id);
	}
	// 1공정 현황 가져오기
	public List<OperationVO> getOperation1Info() throws SQLException{
		return factoryDao.getOperation1Info();
	}
	// 2공정 현황 가져오기
	public List<OperationVO> getOperation2Info() throws SQLException{
		return factoryDao.getOperation2Info();
	}
	// 1공정 현황 가져오기
	public List<OperationVO> getOperation3Info() throws SQLException{
		return factoryDao.getOperation3Info();
	}
	// 공정 현황 맵에 담기
	public HashMap getOperationInfo() throws SQLException{
		HashMap map = new HashMap();
		map.put("op1", factoryDao.getOperation1Info());
		map.put("op2", factoryDao.getOperation2Info());
		map.put("op3", factoryDao.getOperation3Info());
		return map;
	}
	public List<LotVO> elementLotState() throws SQLException {
		return factoryDao.elementLotState();
	}
	// history 정보 가져오기
	public ArrayList<HistoryVO> getHistoryInfo(){
		HistoryVO hvo = new HistoryVO();
		ArrayList<HistoryVO> lhvo = new ArrayList<HistoryVO>();
		
		return lhvo;
	}
	// history 정보 인서트
	public void setHistory() throws SQLException{
		int lot_list_id = factoryDao.getToday_lot_list_id();
		HistoryVO hvo = new HistoryVO();
		hvo.setHis_day_op1_fail(factoryDao.getHis_day_op1_fail(lot_list_id));
		hvo.setHis_day_op2_fail(factoryDao.getHis_day_op2_fail(lot_list_id));
		hvo.setHis_day_op3_fail(factoryDao.getHis_day_op3_fail(lot_list_id));
		hvo.setHis_start(factoryDao.getHis_start(lot_list_id));
		hvo.setHis_end(factoryDao.getHis_end(lot_list_id));
		hvo.setHis_day_ok(factoryDao.getHis_day_ok(lot_list_id));
		hvo.setHis_day_fail(factoryDao.getHis_day_fail(lot_list_id));
		hvo.setHis_op1_hp(factoryDao.getHis_op1_hp(lot_list_id));
		hvo.setHis_op2_hp(factoryDao.getHis_op2_hp(lot_list_id));
		hvo.setHis_op3_hp(factoryDao.getHis_op3_hp(lot_list_id));
		hvo.setHis_lot_total(factoryDao.getHis_lot_total(lot_list_id));
		hvo.setLot_list_id(lot_list_id);
		factoryDao.setHistory(hvo);
	}
	public JSONObject getRefreshInfo() throws SQLException{
		JSONObject obj = new JSONObject();
		try {
			obj.put("chart1", factoryDao.getRefreshInfoDiv1());
			obj.put("chart2", factoryDao.getRefreshInfoDiv2());
			obj.put("chart3", factoryDao.getRefreshInfoDiv3());
			obj.put("chart4", factoryDao.getRefreshInfoDiv4());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return obj;
	}
	// require insert
	public void setRequire(int re_mount, String member_id) throws SQLException{
		RequireVO rvo = new RequireVO();
		rvo.setRe_mount(re_mount);
		rvo.setMember_id(member_id);
		factoryDao.setRequire(rvo);
	}
	// stock insert
	public void setStock(int re_mount, String member_id, String st_location) throws SQLException{
		StockVO svo = new StockVO();
		int lot_list_id = factoryDao.getToday_lot_list_id();
		int st_mount = factoryDao.getSt_mount() + factoryDao.getHis_day_ok(lot_list_id) - re_mount;
		svo.setSt_mount(st_mount);
		svo.setMember_id(member_id);
		svo.setSt_location(st_location);
		System.out.println(svo);
		factoryDao.setStock(svo);
	}
	// stock + require 현황 맵으로...
	public HashMap dayOperResult() throws SQLException{
		HashMap map = new HashMap();
		map.put("req", factoryDao.getBf_require());
		map.put("stock", factoryDao.getBf_stock());
		return map;
	}
	// history 정보 가져오기
	public ArrayList<HistoryVO> getBf_history() throws SQLException{
		return factoryDao.getBf_history();
	}
}
